package com.baibei.ebec.home.shipment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.RaeTabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.android.arouter.utils.TextUtils;
import com.baibei.basic.IPresenter;
import com.baibei.ebec.home.R;
import com.baibei.ebec.home.R2;
import com.baibei.model.Area;
import com.baibei.module.AppRouter;
import com.baibei.module.BasicActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 船列表
 * Created by ChenRui on 2017/8/4 0004 16:08.
 */
@Route(path = AppRouter.ROUTER_AREA_LIST)
public class ShipmentListActivity extends BasicActivity {

    @BindView(R2.id.tablayout)
    RaeTabLayout mTabLayout;
    @BindView(R2.id.viewpager)
    ViewPager mViewpager;

    /**
     * 碎片集合
     */
    private List<Fragment> mFragmentList;
    ViewPagerAdapter mAdapter;

    @Autowired
    public String area;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shipment_area_list);
        ButterKnife.bind(this);

        ARouter.getInstance().inject(this);
        initView();
    }

    private void initView() {
        mFragmentList = new ArrayList<>();
        mFragmentList.add(ShipmentFragment.newInstance(Area.XAU.name()));
        mFragmentList.add(ShipmentFragment.newInstance(Area.XAG.name()));
        mFragmentList.add(ShipmentFragment.newInstance(Area.XPT.name()));

        mAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        mViewpager.setOffscreenPageLimit(3);
        mViewpager.setAdapter(mAdapter);

        //android bug? 2017-8-7  support-design
        mTabLayout.setupWithViewPager(mViewpager, false);
        mTabLayout.removeAllTabs();
        mTabLayout.addTab( mTabLayout.newTab().setText(R.string.gold_jewelry));
        mTabLayout.addTab( mTabLayout.newTab().setText(R.string.silver_jewelry));
        mTabLayout.addTab( mTabLayout.newTab().setText(R.string.platinum_jewelry));
        //跳转
        if (!TextUtils.isEmpty(area)) {
            int position = Area.valueOf(area).ordinal();
            mViewpager.setCurrentItem(position, false);
        }
    }


    @Override
    public IPresenter getBasicPresenter() {
        return null;
    }


    private class ViewPagerAdapter extends FragmentPagerAdapter {

        ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return mFragmentList.get(i);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

    }

}
